﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BalanceCenter_AdminNewsEdit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            int id = -1;
            id = ShoveEL._Convert.StrToInt(this.Request["id"], -1);
            if (id < 0)
            {
                PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);
                return;
            }
            tbID.Text = id.ToString();

            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestCompetences = Competences.BuildCompetencesList(Competences.FillContent, Competences.EditNews);
        RequestLoginPage = "BalanceCenter/AdminNewsEdit.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        DataTable dt = DAL.Tables.T_News.Select("", "[id] = " + tbID.Text, "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (dt.Rows.Count <= 0)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无数据", this.GetType().BaseType.FullName);

            return;
        }

        tbDateTime.Text = dt.Rows[0]["DateTime"].ToString();
        this.ViewState["NewType"] = dt.Rows[0]["Type"].ToString();
        cbisShow.Checked = ShoveEL._Convert.StrToBool(dt.Rows[0]["isShow"].ToString(), true);
        tbTitle.Text = dt.Rows[0]["Title"].ToString();
        try
        {
            tbContent.Value = ShoveEL._String.Decompress((byte[])dt.Rows[0]["Content"]);
        }
        catch { }       
    }

    protected void btnSave_Click(object sender, System.EventArgs e)
    {
        DateTime dtime = System.DateTime.Now;
        try
        {
            dtime = System.DateTime.Parse(tbDateTime.Text);
        }
        catch
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "时间格式错误，请输入如“" + dtime.ToString() + "”的时间格式。");

            return;
        }

        string Title = tbTitle.Text.Trim();
        if (Title == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入标题。");

            return;
        }

        if (DAL.Tables.T_News.Update("[id] = " + tbID.Text, _User.ID, true, dtime, true, Title, true, ShoveEL._String.Compress(tbContent.Value), true, cbisShow.Checked, true, 1, false) < 0)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }
        
        this.Response.Redirect("AdminNews.aspx?NewType=" + ViewState["NewType"].ToString(), true);
    }
}
